import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/home/HomeView.vue'),
      meta: { title: '首页', icon: 'House' },
    },
    {
      path: '/forest-management',
      name: 'forest-management',
      component: () => import('@/views/forest-management/ForestManagementView.vue'),
      meta: { title: '森林经营智能体', icon: 'Setting' },
    },
    {
      path: '/disease-pest',
      name: 'disease-pest',
      component: () => import('@/views/disease-pest/DiseasePestView.vue'),
      meta: { title: '桉树病虫害识别智能体', icon: 'MapLocation' },
    },
    {
      path: '/fire-monitoring',
      name: 'fire-monitoring',
      component: () => import('@/views/fire-monitoring/FireMonitoringView.vue'),
      meta: { title: '森林火灾监测智能体', icon: 'Warning' },
    },
  ],
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  // if ()
  const userStore = useUserStore()
  if (to.path && to.path !== '/' && !userStore.isAuthenticated) {
    const res = await userStore.login()
    if (!res) {
      next('/')
      return
    }
  }
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - ${import.meta.env.VITE_APP_TITLE}`
  }

  next()
})

export default router
